//
//  BinvitiveViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/16.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BinvitiveViewController.h"
#import "BinvitiveRightViewController.h"//候保师傅
#import "BinvitiveLeftViewController.h"//实体店
#import "BManagerViewController.h"//管理商

@interface BinvitiveViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView *contentScrollView;
@property(nonatomic,strong)UIButton *oldButton;
@property(nonatomic,strong)UILabel *seletLabel;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,assign)BOOL isbutton;
@end

@implementation BinvitiveViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super  viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(NSMutableArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isbutton = NO;
    self.title = @"产品管理";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setScrollViewTopandBottom];
    [self setupViewControllers];
    [self setTitlesButton];
    
    //if (!strIsEmpty(_teacher)) {
    //    CGFloat offsetX = ScreenW;
    //    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    //}
    
    CGFloat offsetX = ScreenW*self.index;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    // Do any additional setup after loading the view.
}

-(void)returnaction{
    [self.navigationController popViewControllerAnimated:YES];
}

//初始化标题ScrollView
-(void)setupScrollView{
    NSInteger count = self.childViewControllers.count;
    //设置标题滚动条
    //设置内容滚动条
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*3, 0);
    //开启分页
    self.contentScrollView.pagingEnabled = YES;
    //没有弹簧效果
    self.contentScrollView.bounces = NO;
    //隐藏水平滚动条
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //设置协议
    self.contentScrollView.delegate = self;
}

//设置页面scrollView
-(void)setScrollViewTopandBottom{
    
    UIScrollView *contentScroll = [[UIScrollView alloc]init];
    contentScroll.frame = CGRectMake(0, kNaviHeight, ScreenW, KHEIGHT-kNaviHeight);
    contentScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentScroll];
    self.contentScrollView = contentScroll;
    self.contentScrollView.userInteractionEnabled = YES;
    self.contentScrollView.scrollEnabled = YES;;
    self.contentScrollView.pagingEnabled = YES;
    //显示水平方向的滚动条(默认YES)
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //显示垂直方向的滚动条(默认YES)
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*3, ScreenH-kNaviHeight-44);
    self.contentScrollView.delegate = self;
}

#pragma mark -- UISCrollview代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //CGFloat offsetX = index * ScreenW;
    
    UIButton *but = (UIButton *)[self.view viewWithTag:100+index];
    [self.oldButton setTitleColor:zhutiColor forState:UIControlStateNormal];
    [self.oldButton setBackgroundColor:[UIColor whiteColor]];
    
    [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but setBackgroundColor:zhutiColor];
    
    self.oldButton = but;
}

-(void)setTitlesButton{
    
    NSArray *arr = @[@"侯保师傅",@"管理商",@"实体店"];
    
    CGFloat leftRight = (KWIDTH-180)/2;
    CGFloat wwww = 60;
    CGFloat hhhhh = 30;
    CGFloat yyyy = kNaviHeight -40;
    UILabel *kuangLb = [[UILabel alloc]initWithFrame:CGRectMake(leftRight-1, yyyy-1, 183, 32)];
    kuangLb.layer.borderWidth =1;
    kuangLb.layer.borderColor = zhutiColor.CGColor;
    kuangLb.layer.masksToBounds =YES;
    kuangLb.layer.cornerRadius = 4;
    [self.view addSubview:kuangLb];
    
    UIButton *returnbut = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnbut setImage:[UIImage imageNamed:@"left_icon.png"] forState:UIControlStateNormal];
    returnbut.frame = CGRectMake(10, kNaviHeight-40, 35, 35);
    [returnbut addTarget:self action:@selector(returnaction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnbut];
    
    UILabel *lone = [[UILabel alloc]initWithFrame:CGRectMake(0,  kNaviHeight-1,KWIDTH, 1)];
    lone.backgroundColor = [UIColor colorWithHexString:@"#FFF2F2F2"];
    [self.view addSubview:lone];
    
    for (int i = 0; i<arr.count; i++) {
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [myButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        myButton.frame = CGRectMake((wwww)*i+leftRight, yyyy, wwww, hhhhh);
        [self.view addSubview:myButton];
        myButton.tag = 100+i;
        [myButton setTitle:arr[i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [myButton setTitleColor:zhutiColor forState:UIControlStateNormal];
        [myButton setBackgroundColor:[UIColor whiteColor]];
        myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        //        myButton.layer.borderWidth =1;
        //        myButton.layer.borderColor = zhutiColor.CGColor;
        if (i == 0) {
            
            [myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [myButton setBackgroundColor:zhutiColor];
            
            self.oldButton = myButton;
            [self titleClick:myButton];
            [self   cutCornerRadius:myButton andCornerRadii:CGSizeMake(4, 4) andAtRectCorner:UIRectCornerTopLeft];
            [self   cutCornerRadius:myButton andCornerRadii:CGSizeMake(4, 4) andAtRectCorner:UIRectCornerBottomLeft];
            
        } else {
            [self   cutCornerRadius:myButton andCornerRadii:CGSizeMake(4, 4) andAtRectCorner:UIRectCornerTopRight];
            [self   cutCornerRadius:myButton andCornerRadii:CGSizeMake(4, 4) andAtRectCorner:UIRectCornerBottomRight];
        }
    }
}

-(void)titleClick:(UIButton *)but{
    //0取出label
    NSInteger index = but.tag-100;
    //2.1计算滚动位置
    CGFloat offsetX = index * ScreenW;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

//显示页面
-(void)showVC:(NSInteger)index{
    
    CGFloat offsetX = index * ScreenW;
    
    UIViewController *vc = self.childViewControllers[index];
    
    //判断当前控制器的View 有没有加载过 如果已经加载过 就不需要加载
    if (vc.isViewLoaded) return;
    vc.view.frame = CGRectMake(offsetX, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:vc.view];
}

- (void)setupViewControllers {
    
    BinvitiveRightViewController *evaluate = [[BinvitiveRightViewController alloc]init];
    evaluate.view.frame = CGRectMake(0, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:evaluate.view];
    [self addChildViewController:evaluate];
    
    BManagerViewController *bManagerVC = [BManagerViewController new];
    bManagerVC.view.frame = CGRectMake(ScreenW, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:bManagerVC.view];
    [self addChildViewController:bManagerVC];
    
    BinvitiveLeftViewController *seveVC = [[BinvitiveLeftViewController alloc]init];
    seveVC.view.frame = CGRectMake(ScreenW*2, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:seveVC.view];
    [self addChildViewController:seveVC];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)cutCornerRadius:(UIView *)view andCornerRadii:(CGSize)cornerRadii andAtRectCorner:(UIRectCorner)corners{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
